import React from "react";
import axios from "./axios.js";

class Login extends React.Component {
  state = {
    user: "ggt",
    psw: "123",
  };
  submitForm = async () => {
    const res = await axios.post("v1/user/login", {
      us: this.state.user,
      ps: this.state.psw,
    });
    console.log(res);
    if (res.err === 0) {
      console.log(res.info);
      // localStorage.removeItem("info");
      localStorage.setItem("token", JSON.stringify(res.info.token));
      localStorage.setItem("uid", JSON.stringify(res.info._id));
    }
    return;
  };
  render() {
    const { user, psw } = this.state;
    return (
      <div>
        <h1>登录</h1>
        <form>
          用户名
          <input
            type="text"
            placeholder="Please input"
            value={user}
            onChange={(e) => {
              this.setState({ user: e.target.value });
            }}
          />
          密码
          <input
            type="password"
            placeholder="Please input"
            value={psw}
            onChange={(e) => {
              this.setState({ psw: e.target.value });
            }}
          />
          <button onClick={this.submitForm}>提交</button>
        </form>
      </div>
    );
  }
}

export default Login;
